<template>
  <el-dialog :title="title" :visible.sync="visible" width="1100px" append-to-body custom-class="import-dialog"
             @close="close"
  >
    <el-row :gutter="10" class="mb20">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          :disabled="disabledState"
        >新增合同
        </el-button
        >
      </el-col>
      <!--      <right-toolbar-->
      <!--        :showSearch.sync="showSearch"-->
      <!--        @queryTable="getList"-->
      <!--      ></right-toolbar>-->
    </el-row>
    <el-table
      v-loading="loading"
      :data="list"
      ref="table"
    >
      <el-table-column label="系统编号" prop="lesseeContractId"/>
      <el-table-column label="签署方式" prop="signingMode">
        <template #default="scope">
          <dict-tag
            :options="dict.type.lessee_signing_mode"
            :value="scope.row.signingMode"
          />
        </template>
      </el-table-column>
      <el-table-column label="合同分类" prop="contractType">
        <template #default="scope">

          <dict-tag
            :options="dict.type.contract_type"
            :value="scope.row.contractType"
          />
        </template>
      </el-table-column>
      <el-table-column label="租住周期" prop="contractTemplateName">
        <template #default="scope">
          <div>入：{{ scope.row.checkInTime|formatDate('YYYY/MM/DD') }}</div><div>离：{{ scope.row.checkOutTime|formatDate('YYYY/MM/DD') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="最晚签约日期" prop="latestSigningTime"/>
      <el-table-column label="状态" prop="lesseeContractStatus">
        <template #default="scope">
          <dict-tag
            :options="dict.type.lessee_contract_status"
            :value="scope.row.lesseeContractStatus"
          />
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新增 -->
    <Add v-model="addDialogVisible" :title="addTitle" @success="getList" :form-data="form" :isSigning="true"/>
  </el-dialog>
</template>

<script>
import { listLesseeContract } from '@/api/contract/lessee'
import Add from '@/views/long-short-rent/contract/lessee/components/add.vue'

export default {
  name: 'contract-info',
  components: { Add },
  dicts: ['contract_type', 'lessee_contract_status', 'lessee_signing_mode'],
  props: {
    value: {
      default: false
    },
    title: {
      default: '合同信息'
    },
    data: {
      default: () => {}
    }
  },
  data() {
    return {
      //是否加载
      loading: false,
      // 是否显示搜索框
      showSearch: false,
      // 新增弹窗是否显示
      addDialogVisible: false,
      addTitle: '新增合同',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    disabledState(){
      return ['10','11'].includes(this.data.orderState)
    }
  },
  watch: {
    value: {
      handler(newVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    init() {
      this.reset()
      this.getList()
    },
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        orderId: this.data.orderId
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listLesseeContract(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 新增合同 */
    handleAdd() {
      if(this.disabledState){
        this.$message.error('订单已结束，创建失败')
        return
      }
      if(this.data.newContractFlag === '1'){
        this.$message.error('订单有进行中或签署完成的合同，创建失败')
        return
      }
      this.form = {
        orderId: this.data.orderId,
        checkInTime: this.data.checkInTime,
        checkOutTime: this.data.checkOutTime,
        houseId: this.data.houseId,
        premisesAddress: this.data.premisesAddress,
        cooperationType: this.data.cooperationType,
      }
      this.addTitle = '新增合同'
      this.addDialogVisible = true

    },
    //关闭
    close() {
      this.visible = false
    }
  }
}
</script>

<style scoped lang="scss">

</style>
